<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 自定义组件，标题 -->
      <PageTitle :show-before="true">
        <template v-slot:before>
          <h2>{{ $t('route.hotSearch') }}</h2>
        </template>
        <template v-slot:after>
          <el-button class="filter-item" size="medium" style="margin-left: 10px;" type="primary" icon="el-icon-edit" :disabled="!checkPermission('hotSearch-add')" @click="addOrEidt('add',0)">{{ $t('button.add') }}</el-button>
        </template>
      </PageTitle>
      <!-- 搜索筛选 -->
      <el-row :gutter="20" style="margin-top:20px">
        <el-col :span="4">
          <el-input v-model="query.name" type="text" :placeholder="$t('table.ForeignLanguageName')" />
        </el-col>
        <el-col :span="3">
          <el-button type="primary" icon="el-icon-search" @click="search">{{ $t('button.search') }}</el-button>
        </el-col>
      </el-row>
      <!-- 数据表 -->
      <el-table v-loading="downloadLoading" border :data="list" max-height="650px" style="margin-top: 20px;">
        <el-table-column label="ID" prop="id" sortable="custom" width="60" />
        <el-table-column :label="$t('table.ForeignLanguageName')" prop="name" />
        <el-table-column :label="$t('table.ChName')" prop="sub_name" />
        <el-table-column label="搜索次数" prop="sum" width="180" />

        <el-table-column :label="$t('table.creationTime')" prop="created_at">
          <template slot-scope="{row}">
            {{ row.created_at|timeStanp }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.actions')" align="center" width="220" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button icon="el-icon-edit" type="primary" size="mini" :disabled="!checkPermission('hotSearch-edit')" @click="addOrEidt('edit',row)">{{ $t('button.edit') }}</el-button>
            <el-button icon="el-icon-delete" type="dang er" size="mini" :disabled="!checkPermission('hotSearch-del')" @click="delData(row.id)">{{ $t('button.delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" align="middle" style="margin-top:20px">
        <el-pagination background layout="total,prev,pager,next" :total="count" :page-size="query.limit" :current-page="query.page" @current-change="changPage" />
      </el-row>
    </div>
    <!-- 添加 -->
    <el-dialog :title="dialogTitle" :visible="showDialog" @close="btnCanle">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="150px">
        <el-form-item :label="$t('table.ForeignLanguageName')" prop="name">
          <el-input v-model="formData.name" :placeholder="$t('table.enterThe')+' '+$t('table.ForeignLanguageName')" style="width:50%" />
        </el-form-item>
        <el-form-item :label="$t('table.ChName')">
          <el-input v-model="formData.sub_name" :placeholder="$t('table.enterThe')+' '+$t('table.ChName')" style="width:50%" />
        </el-form-item>
        <el-form-item label="搜索次数" prop="sum">
          <el-input v-model.number="formData.sum" :placeholder="$t('table.enterThe')+'搜索次数'" style="width:50%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnCanle">{{ $t('button.cancel') }}</el-button>
        <el-button type="primary" @click="btnOk">{{ $t('button.confirm') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { HotSearchList, addHotSearch, delShopGroup } from '@/api/user'
import user from '@/store/modules/user'
export default ({
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      downloadLoading: false,
      statusData: [{
        id: 1,
        value: this.$t('table.normal')
      },
      {
        id: 2,
        value: this.$t('table.deactivate')
      }],
      groupData: [{
        id: 1,
        value: this.$t('table.list')
      },
      {
        id: 2,
        value: this.$t('table.row')
      }],
      query: {
        name: '',
        aid: 1,
        page: 1,
        limit: 10
      },
      count: 0,
      dialogTitle: this.$t('button.add'),
      formData: {
        aid: 1,
        id: undefined,
        name: '',
        sub_name: '',
        sum: undefined
      },
      rules: {
        name: [{ required: true, message: this.$t('table.name') + this.$t('prompt.noEmpty'), trigger: 'blur' }],
        sum: [{ required: true, message: '搜索次数' + this.$t('prompt.noEmpty'), trigger: 'blur' }]
      },
      showDialog: false
    }
  },
  created() {
    // 获取userid
    this.getBusinessCateList()
    this.query.bid = parseFloat(user.state.userInfo.bid)
    this.formData.bid = parseFloat(user.state.userInfo.bid)
  },
  methods: {
    // 获取商户分了列表
    async getBusinessCateList() {
      this.downloadLoading = true
      const { count, list } = await HotSearchList(this.query)
      this.count = count
      // 给数组对象使用map循环添加新属性
      const lists = list.map((item, index) => {
        return Object.assign(item, { isedit: false })
      })
      this.list = lists
      this.downloadLoading = false
    },
    // 点击展开新增修改
    addOrEidt(type, row) {
      if (type === 'add') {
        this.dialogTitle = this.$t('button.add')
        this.formData.id = undefined
        this.formData.name = ''
        this.formData.sub_name = ''
        this.formData.sum = undefined
        this.showDialog = true
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      } else if (type === 'edit') {
        this.dialogTitle = this.$t('button.edit')
        this.formData.id = row.id
        this.formData.name = row.name
        this.formData.sum = row.sum
        this.formData.sub_name = row.sub_name
        this.showDialog = true
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
    },
    // 点击确定时
    async btnOk() {
      try {
        // 校验整个表单
        await this.$refs.ruleForm.validate()
        // 调用新增接口
        await addHotSearch(this.formData)
        // 更新数据
        this.getBusinessCateList()
        this.btnCanle()
        this.$message.success(this.$t('prompt.success'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 删除管理员
    async delData(e) {
      const data = {
        id: e
      }
      try {
        await this.$confirm(this.$t('prompt.deleteit'))
        await delShopGroup(data)
        this.getBusinessCateList()
        this.$message.success(this.$t('prompt.deletesuccess'))
      } catch (error) {
        this.$message.error(error)
      }
    },
    // 取消重置原来的数据
    btnCanle() {
      this.formData = {
        aid: 1,
        id: undefined,
        name: '',
        sub_name: '',
        sum: undefined
      }
      this.$refs.ruleForm.clearValidate()// 重置校验结果
      this.showDialog = false
    },
    // 搜索
    search() {
      this.query.page = 1
      this.getBusinessCateList()
    },
    // 分页
    changPage(newPage) {
      this.query.page = newPage
      this.getBusinessCateList()
    }
  }
})
</script>
<style lang="scss">
.table-img{
  width: 90px;
}
</style>
